﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Web
{
    public class BasePage:Page
    {
           #region 权限Code属性
        /// <summary>
        /// 私有变量-权限代码,在子类中必须初始化的
        /// </summary>
        private string _Authority_Code;
        /// <summary>
        /// 设置权限编号，要在子类的构造函数里设置
        /// </summary>
        public string Authority_Code
        {
            set { _Authority_Code = value; }
            get { return _Authority_Code; }
        }
        /// <summary>
        /// 登用用户角色ID
        /// </summary>
        private int _Role_Id;
        /// <summary>
        /// 判断登录用户角色ID
        /// </summary>
        public int Role_ID
        {
            set { _Role_Id = value; }
            get { return _Role_Id; }
        }
        /// <summary>
        /// 登录用户ID
        /// </summary>
        private int _Admin_Id;
        /// <summary>
        /// 判断登录用户ID
        /// </summary>
        public int Admin_ID
        {
            set { _Admin_Id = value; }
            get { return _Admin_Id; }
        }
        #endregion

        #region  构造函数
        public BasePage()
        {
            this.Load += new EventHandler(UBasePage_Load);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="popCode">权限代码</param>
        public BasePage(string popCode)
        {
            _Authority_Code = popCode;
            this.Load += new EventHandler(UBasePage_Load);
        }
        #endregion
        /// <summary>
        /// LOAD事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UBasePage_Load(object sender, EventArgs e)
        {
            CheckAdminAuthority();
        }
        public int canVisit()
        {
            // 0 没登录， 1 登录成功
            if (HttpContext.Current.Session["adminId"] == null)
                return 0;
            return 1;

        }
        /// <summary>
        /// 判断登录权限
        /// </summary>
        protected void CheckAdminAuthority()
        {
            int result = 0;
            result = canVisit();
            if (result == 0)
            {
                HttpContext.Current.Response.Redirect("login.aspx");
            }
            //角色ID
            int curRole = Int32.Parse(HttpContext.Current.Session["adminRole_Id"].ToString());
            //超级管理员权限判断
            if (curRole == 1)
            {
                return;
            }

            #region 其他权限判断
            BLL.Roletopermissions biz_perm = new BLL.Roletopermissions();
            //角色下权限列表
            DataSet ds_Role_Perm = biz_perm.GetList(" Role_id=" + curRole);
         
            bool Perm_logined = false;
            string Role_Perm = string.Empty;
            //操作权限判断
            for (int j = 0; j < ds_Role_Perm.Tables[0].Rows.Count; j++)
            {
                Role_Perm = ds_Role_Perm.Tables[0].Rows[j]["Permiss_Code"].ToString();
                //判断操作权限是不在列表中
                if (Role_Perm.Contains(_Authority_Code))
                {
                    Perm_logined = true;
                    break;
                }
            }
            //操作权限
            if (Perm_logined == false)
            {
                //跳转失败操作权限页面
                //Response.Redirect("do_error.aspx");
            }
            #endregion
        }


        /// <summary>
        /// 判断登录权限(div的显示与隐藏eg：menu显示)
        /// </summary>
        protected bool CheckAdminAuthority(string code)
        {
          
            //角色ID
            int curRole = Int32.Parse(HttpContext.Current.Session["adminRole_Id"].ToString());

            //超级管理员权限判断
            if (curRole == 1)
            {
                return true;
            }

            #region 其他权限判断
            BLL.Roletopermissions biz_perm = new BLL.Roletopermissions();
            //角色下权限列表
            DataSet ds_Role_Perm = biz_perm.GetList(" Role_id=" + curRole);
           
            string Role_Perm = string.Empty;
            //操作权限判断
            for (int j = 0; j < ds_Role_Perm.Tables[0].Rows.Count; j++)
            {
                Role_Perm = ds_Role_Perm.Tables[0].Rows[j]["Permiss_Code"].ToString();
                //判断操作权限是不在列表中
                if (Role_Perm.Contains(code))
                {
                    return true;
                }
                else
                { return false; }
            }
            return false;
            #endregion
        }

    }
}
